package com.shchuangzhi.smartguard;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.opengl.Visibility;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.shchuangzhi.smartguard.core.Consts;
import com.shchuangzhi.smartguard.core.DBHelper;
import com.shchuangzhi.smartguard.core.User;
import com.shchuangzhi.smartguard.core.Util;

public class LoginActivity extends AppCompatActivity {

    private EditText editPwd;
    private EditText editAccount;
    private Button btnReset;

    private int cilckCount;
    private static final int SHOW_RESET_CLICK_COUNT = 10;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        editAccount = (EditText) findViewById(R.id.txt_account);
        editPwd = (EditText) findViewById(R.id.txt_pwd);

        Button btnSignIn = (Button) findViewById(R.id.btn_sign_in);
        btnSignIn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                String account = editAccount.getText().toString();
                String pwd = Util.getMD5(editPwd.getText().toString() + Consts.PASSWORD_SALT);
                attemptLogin(account, pwd);
            }
        });
        btnReset = (Button)findViewById(R.id.btn_reset);
        btnReset.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(LoginActivity.this).
                        setTitle(getString(R.string.confirm_to_reset)).
                        setMessage(getString(R.string.confirm_to_reset_message)).
                        setPositiveButton(getString(R.string.button_confirm), new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                new DBHelper(LoginActivity.this).reset();
                                Toast.makeText(LoginActivity.this, getString(R.string.reset_ok), Toast.LENGTH_LONG).show();
                            }
                        }).
                        setNegativeButton(getString(R.string.button_cancel), new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        }).
                        create().show();
            }
        });

        LinearLayout hideLayout = (LinearLayout)findViewById(R.id.hide_layout);
        hideLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cilckCount > SHOW_RESET_CLICK_COUNT) {
                    btnReset.setVisibility(View.VISIBLE);
                    cilckCount = 0;
                } else {
                    cilckCount++;
                }
            }
        });

        SharedPreferences sp = getSharedPreferences(Consts.APP_FULL_NAME, Context.MODE_PRIVATE);
        boolean isAutoLogin = sp.getBoolean(Consts.PREF_KEY_LOGIN_AUTO, false);
        if (isAutoLogin) {
            String account = sp.getString(Consts.PREF_KEY_LOGIN_ACCOUNT, "");
            String pwd = sp.getString(Consts.PREF_KEY_LOGIN_PWD, "");
            attemptLogin(account, pwd);
        }
    }

    private void attemptLogin(String account, String pwd) {
        User usr = User.create(this);
        User.LoginResult result = usr.Login(account, pwd);

        if (!result.OK()) {
            Toast.makeText(getApplicationContext(), result.Message(),
                    Toast.LENGTH_SHORT).show();
        } else {
            CheckBox ckb = (CheckBox)findViewById(R.id.ckb_auto_login);
            if (ckb.isChecked()) {
                SharedPreferences sp = getSharedPreferences(Consts.APP_FULL_NAME, Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putBoolean(Consts.PREF_KEY_LOGIN_AUTO, true);
                editor.putString(Consts.PREF_KEY_LOGIN_ACCOUNT, account);
                editor.putString(Consts.PREF_KEY_LOGIN_PWD, pwd);
                editor.commit();
            }

            btnReset.setVisibility(View.INVISIBLE);

            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            this.finish();
        }
    }
}

